package source.model;

import source.events.action.Action;
import source.model.type.*;
import source.Driver;
import java.util.*;
import source.view.MainScreen;
import source.events.action.*;

public class CountDownItem extends Item implements Useable {
	
	private int turnsLeft;
	private boolean isCounting = false;

	public CountDownItem() {
		super( ItemType.COUNTDOWNMINE );
		turnsLeft = 3 * Driver.numPlayers;
	}
	
	public void startCountDown() {
		isCounting = true;
	}
	
	public boolean isCounting() {
		return isCounting;
	}
	
	public Action decrementTurn() {
		if (!this.isCounting) {
			return new Action() {
				public ActionResult execute( DataHandler dh ) {
					return ActionResult.SUCCESS;
				}
			};
		}
		turnsLeft--;
		if (turnsLeft == 0) {
			final ObjectID thisItem = this.objectID();
			return new Action() {
				public ActionResult execute( DataHandler dh ) {
					Vector<Position> positionsAffected=new Vector<Position>();
					Vector<Unit> unitsAffected=new Vector<Unit>();
					Position p=dh.positionQuery(thisItem);
					positionsAffected.add(p);
					positionsAffected.add(new Position(p.getX()-1	, p.getY()-1));
					positionsAffected.add(new Position(p.getX()		, p.getY()-1));
					positionsAffected.add(new Position(p.getX()+1	, p.getY()-1));
					positionsAffected.add(new Position(p.getX()-1	, p.getY()  ));
					positionsAffected.add(new Position(p.getX()+1	, p.getY()  ));
					positionsAffected.add(new Position(p.getX()-1	, p.getY()+1));
					positionsAffected.add(new Position(p.getX()		, p.getY()+1));
					positionsAffected.add(new Position(p.getX()+1	, p.getY()+1));
					for(Position pos : positionsAffected){
						if(!pos.isValid())
							continue;
						GameObject[] go=dh.positionQuery(pos, GameObjectType.UNIT);
						Unit[] u=new Unit[go.length];
						for(int i=0; i<u.length; i++)
							u[i]=(Unit)go[i];
						for(int i=0; i<u.length; i++)
							unitsAffected.add(u[i]);
					}
					if(unitsAffected.size()>0){
						for(Unit u:unitsAffected){
							u.modHealth(-20);
						}
					}
					MainScreen.writeToConsole("Item blew up!!!");
					ObjectID itemThatBlewUp = thisItem;
					GameObject itbu = dh.objectQuery(itemThatBlewUp);
					itbu.invalidate();
					dh.returnObject(itbu);
					// This is for testing purposes to see if item blew up
					if (dh.objectQuery(itemThatBlewUp) == null) {
						MainScreen.writeToConsole("SUCCESS!!!");
					}
					return ActionResult.SUCCESS;
				}
			};
		}
		else {
			return new Action() {
				public ActionResult execute( DataHandler dh ) {
					return ActionResult.SUCCESS;
				}
			};
		}
	}
	
	public Action use() {
		final ObjectID thisItem = this.objectID();
		return new Action() {
			ObjectID itemID = thisItem;
			public ActionResult execute( DataHandler dh ) {
				
				GameObject go = dh.getCurrentSelection();
				if ( go.objectType() != GameObjectType.UNIT ) return ActionResult.IMPOSSIBLE;
				
				Unit unit = (Unit)go;
				
				DropItemAction dia=new DropItemAction();
				dia.execute(dh);
				unit.removeItem();
				GameObject fluxItem = dh.objectQuery( itemID );
				CountDownItem cdi = (CountDownItem)fluxItem;
				
				cdi.startCountDown();
				
				Position newPos = dh.positionQuery( unit.objectID() );
				dh.positionCommand( cdi.objectID(), newPos );
				
				return ActionResult.SUCCESS;
			}
		};
	}

}
